
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doghost.controllers;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import doghost.DoghostApp;
import doghost.editors.RacaEditor;
import doghost.entities.Raca;

/**
 *
 * @author duo
 */
public class RacaController {
    protected Raca raca;
    protected boolean operationOk;

    public Raca getRaca() {
        return raca;
    }

    public void setRaca(Raca raca) {
        this.raca = raca;
    }

    /**
     * Get the value of operationOk
     *
     * @return the value of operationOk
     */
    public boolean isOperationOk() {
        return operationOk;
    }

    /**
     * Set the value of operationOk
     *
     * @param operationOk new value of operationOk
     */
    public void setOperationOk(boolean operationOk) {
        this.operationOk = operationOk;
    }

    public void newRaca(EntityManager entityManager, List list) {
        // create a transient object
        this.setOperationOk(false);
        setRaca(new Raca());
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        RacaEditor racaEditor = new RacaEditor(mainFrame, true);
        racaEditor.setLocationRelativeTo(mainFrame);
        racaEditor.setCurrentRecord(getRaca());
        racaEditor.setVisible(true);
        if (racaEditor.isSaveFired()) {
            try {
                entityManager.getTransaction().begin();
                entityManager.persist(getRaca());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.add(getRaca());
                }
                Logger.getLogger(this.getClass().getName()).
                        log(Level.INFO, "Raca adicionado(a) com sucesso: " +
                        getRaca());
            }
            catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Erro adicionando Raca: " + e);
            }
        }
    }

    public void editRaca(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        entityManager.getTransaction().begin();
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        RacaEditor racaEditor = new RacaEditor(mainFrame, true);
        racaEditor.setLocationRelativeTo(mainFrame);
        racaEditor.setCurrentRecord(getRaca());
        racaEditor.setVisible(true);
        if (racaEditor.isSaveFired()) {
            try {
                entityManager.merge(getRaca());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, "Raca Editado(a) com sucesso: " +
                    getRaca());
            } catch (RollbackException rex) {
                // refresh raca in list
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Raca erro de edição (rollback) : " +
                    rex);
                Raca originalRaca = (Raca)
                        entityManager.find(getRaca().getClass(),
                        getRaca().getId());
                if(list != null) {
                    list.remove(getRaca());
                    list.add(index, originalRaca);
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Raca erro de edição: " + e);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando edição de raca, " +
                                "transação ativa: " + erb);
                    }
                }
            }
        } else {
            try {
                if (entityManager.getTransaction().isActive()) {
                    entityManager.refresh(getRaca());
                    entityManager.getTransaction().commit();
                }
                // refresh raca in list
                Raca originalRaca = (Raca)
                        entityManager.find(getRaca().getClass(),
                        getRaca().getId());
                if(list != null) {
                    list.remove(getRaca());
                    list.add(index, originalRaca);
                }
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, " Edição de Raca cancelada com sucesso " +
                    "raca: " + getRaca());
            } catch (RollbackException rex) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro cancelando edição de raca " +
                    "(rollback) : " + rex);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando eliminação de raca. " +
                                "transação ativa: " + erb);
                    }
                }
            }
        }
    }

    public void deleteRaca(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        Object[] options = {"Sim", "Não"};
        int n = JOptionPane.showOptionDialog(null, "Remove a linha ?", "Aviso",
                JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, null);
        if (n ==JOptionPane.YES_OPTION) {
            try {
                entityManager.getTransaction().begin();
                entityManager.remove(getRaca());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.remove(getRaca());
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro eliminando raca: " + e);
            }
        }
    }


}

